//
//  HXMainController.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/11/23.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

class HXMainController: UITabBarController {
    
    func composeClick()
    {
        print(__FUNCTION__)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        //设置自定义tabBar
        let newTabBar = HXTabBar()
        //KVC
        setValue(newTabBar, forKey: "tabBar")
        
        //为tabBar中间的加号按钮添加点击事件
        newTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置标题颜色,该方法仅限于tabBarItem的文字和图片的颜色一致的情况下
        tabBar.tintColor = UIColor.orangeColor()
        
        //创建首页VC
        let homeVC = HXHomeTableViewController()
        self.addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //创建消息VC
        let messageVC = HXMessageTableViewController()
        self.addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        //创建发现VC
        let discoverVC = HXDiscoverTableViewController()
        self.addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //创建我VC
        let profileVC = HXProfileTableViewController()
        self.addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    // MARK - 抽取方法
    func addChildViewController(tableViewController : UITableViewController, title : String, imageName : String) {
        tableViewController.title = title
        tableViewController.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(UINavigationController(rootViewController: tableViewController))
        
        //设置选择状态下的图片变化
        let selectedHightedImageName = imageName + "_highlighted"
        //取消系统渲染
        let selectedHightedImage = UIImage(named: selectedHightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        tableViewController.tabBarItem.selectedImage = selectedHightedImage;
    }
}
